﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.Entity;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;

namespace ShopB2C.SQLDataAccess
{
    public class ShopCarAccess
    {
        /// <summary>
        /// 增加到购物车
        /// </summary>
        private const string _addToCar = "ShopCar_Append";
        /// <summary>
        /// 获取用户购物车
        /// </summary>
        private const string _carList = "ShopCar_Select";
        /// <summary>
        /// 删除购物车
        /// </summary>
        private const string _deleteCar = "ShopCar_Delete";
        /// <summary>
        /// 更新购物车
        /// </summary>
        private const string _updateCar = "ShopCar_Update";
        /// <summary>
        /// 存放于Cookie中的带有自定义属性的产品
        /// </summary>
        private const string _cookProduct = "ShopCar_CookProduct";
        /// <summary>
        /// 移动到指定的类型
        /// </summary>
        private const string _moveToType = "ShopCar_MoveToType";
        /// <summary>
        /// 复制到指定的类型
        /// </summary>
        private const string _copyToType = "ShopCar_CopyToType";
        private bool _isCN = true;

        public ShopCarAccess(string connectionString, string langName)
        {
            SQLProcedureHelper.ConnectionString = connectionString;
            this._isCN = langName.ToLower() == "en-us" ? false : true;
        }
        /// <summary>
        /// 增加到购物车
        /// </summary>
        /// <returns>受影响的行</returns>
        public int AddToCar(ShopCarEntity car)
        {
            int result = 0;
            SqlParameter[] parameters = 
            {
                new SqlParameter("@Description",SqlDbType.NVarChar){ Value=car.Description },
                new SqlParameter("@Count",SqlDbType.Int){ Value=car.Count },
                new SqlParameter("@Uid",SqlDbType.Int){ Value=car.Uid },
                new SqlParameter("@ProductId",SqlDbType.Int){ Value=car.ProductId },
                new SqlParameter("@Property",SqlDbType.VarChar,4000){ Value=car.Property },
                new SqlParameter("@Type",SqlDbType.Int){Value = car.CartType}
            };
            result = SQLProcedureHelper.ExecuteNonQuery(string.Format("{0}{1}", _addToCar, _isCN ? string.Empty : "_en"), parameters);
            return result;
        }
        /// <summary>
        /// 更新购物车
        /// </summary>
        /// <param name="count">商品数量</param>
        /// <param name="productId">商品ID</param>
        /// <param name="uid">用户ID</param>
        /// <returns>受影响的行</returns>
        public int UpdateCar(int productId, int count, string property, int uid, int type)
        {
            int result = 0;
            SqlParameter[] parameters = 
            {
                new SqlParameter("@ProductId",SqlDbType.Int){Value = productId}
                ,new SqlParameter("@Count",SqlDbType.Int){Value = count}
                ,new SqlParameter("@Uid",SqlDbType.Int){Value = uid}
                , new SqlParameter("@Type",SqlDbType.Int){Value =type}
                ,new SqlParameter("@Property",SqlDbType.VarChar,4000){Value = property}
            };
            result = SQLProcedureHelper.ExecuteNonQuery(string.Format("{0}{1}", _updateCar, _isCN ? string.Empty : "_en"), parameters);

            return result;
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <returns>受影响的行</returns>
        public int DeleteCar(int productId, string property, int uid, int type)
        {
            int result = 0;
            SqlParameter[] parameters = 
            {
                new SqlParameter("@ProductId",SqlDbType.Int){Value = productId},
                new SqlParameter("@Uid",SqlDbType.Int){Value = uid },
                new SqlParameter("@Type",SqlDbType.Int){Value = type},
                new SqlParameter("@Property",SqlDbType.VarChar,4000){Value = property}
            };
            result = SQLProcedureHelper.ExecuteNonQuery(string.Format("{0}{1}", _deleteCar, _isCN ? string.Empty : "_en"), parameters);

            return result;
        }
        /// <summary>
        /// 购物车列表
        /// </summary>
        /// <returns>购物车列表</returns>
        public List<EProduct> CarList(int uid, int type)
        {
            List<EProduct> shopCarList = new List<EProduct>();
            SqlParameter[] parameters = 
            {
                new SqlParameter("@Uid",SqlDbType.Int){Value = uid},
                new SqlParameter("@Type",SqlDbType.Int){Value = type}
            };

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(string.Format("{0}{1}", _carList, _isCN ? string.Empty : "_en"), parameters);
            while (reader.Read())
            {
                shopCarList.Add(new EProduct()
                {
                    ProductId = Convert.ToInt32(reader["ProductId"]),
                    ProductName = reader["ProductName"].ToString(),
                    DisCount = Convert.ToDecimal(reader["DisCount"]),
                    MyDisCount = Convert.ToDecimal(reader["MyDisCount"]),
                    ImageName = reader["ImageName"].ToString(),
                    Price = Convert.ToDecimal(reader["Price"]),
                    ProductNo = reader["ProductNo"].ToString(),
                    SalesCount = Convert.ToInt32(reader["Count"]),
                    KeyWords = reader["Property"].ToString(),
                    Description = reader["CustomerProperty"].ToString()
                });
            }
            reader.Close();

            return shopCarList;
        }
        /// <summary>
        /// 存放于cookie中带有自定义属性的产品
        /// </summary>
        /// <param name="productId">关联的产品ID</param>
        /// <param name="property">自定属性</param>
        /// <returns>带有自定义属性信息的产品</returns>
        public EProduct GetProductByCookId(int productId, string property, int type)
        {
            EProduct product = null;

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(
                string.Format("{0}{1}", _cookProduct, !_isCN ? "_En" : string.Empty)
                , new SqlParameter[]{
                    new SqlParameter("@ProductId",SqlDbType.Int){Value = productId}
                    , new SqlParameter("@Property",SqlDbType.VarChar,4000){Value=property}
                });

            if (reader.HasRows)
            {
                product = new EProduct();
                reader.Read();
                product.ProductId = productId;
                product.ProductName = reader["ProductName"].ToString();
                product.DisCount = Convert.ToDecimal(reader["DisCount"]);
                product.ImageName = reader["ImageName"].ToString();
                product.Price = Convert.ToDecimal(reader["Price"]);
                product.ProductNo = reader["ProductNo"].ToString();
                product.KeyWords = property;
                product.Description = reader["CustomerProperty"].ToString();
            }

            reader.Close();

            return product;
        }
        /// <summary>
        /// 移动到指定的类型
        /// </summary>
        /// <param name="shopCarEntity">原始消息</param>
        /// <param name="cartType">指定类型</param>
        /// <returns>受影响的行</returns>
        public int MoveToType(ShopCarEntity shopCarEntity, int cartType)
        {
            int result = 0;
            SqlParameter[] parameters =
            {
                new SqlParameter("@ProductId",SqlDbType.Int){Value = shopCarEntity.ProductId },
                new SqlParameter("@uid",SqlDbType.Int){Value = shopCarEntity.Uid },
                new SqlParameter("@Property",SqlDbType.VarChar,4000){Value = shopCarEntity.Property },
                new SqlParameter("@OldType",SqlDbType.Int){Value = shopCarEntity.CartType },
                new SqlParameter("@Type",SqlDbType.Int){Value = cartType }
            };

            result = SQLProcedureHelper.ExecuteNonQuery(_moveToType, parameters);
            return result;
        }

    }
}
